package admingui;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import server.Game;
import server.Server;

public class Admin {

    private String username;
    private String password;
    private Server server;
    private boolean access;
    private Calendar curr;


    Admin(String u, String p, Server s) {
        username = u;
        password = p;
        server = s;
        access = false;
    }


    public void login(String u, String p) {
        access = (u.equals(username) && p.equals(password));
        if (access) {
            System.out.println("Access Granted");

        }
        else {
            System.out.println("You do not have access");
        }
    }


    public void kill() {
        server.kill();
    }


    public void updateEventsLive(String statement, int index) {
        if (access) {
        Game changed = server.getLiveGames()[index];
        changed.events+=statement;
        server.setLiveGames(index,changed);
        }

        else {
            System.out.println("You do not have access.");
        }
    }


    public void updateLiveScore(int scoreA, int scoreB, int index) {
        if (access) {
        Game changed = server.getLiveGames()[index];
        changed.score1 = scoreA;
        changed.score2 = scoreB;
        server.setLiveGames(index, changed);
        }
        else {
            System.out.println("You do not have access.");
        }
    }


    public void updateGameScore (int gameNo, int score1, int score2) {
        curr = Calendar.getInstance();
        server.getDb().updateGameScore(gameNo, score1, score2, curr.HOUR+":"+curr.MINUTE);
    }


    public void updateGameEvents (int gameNo, String events) {
        curr = Calendar.getInstance();
        server.getDb().updateGameEvents(gameNo, events, curr.HOUR+":"+curr.MINUTE);
    }


    public void updateTeamPoints (boolean allTeams, int gameNo) {
        server.getDb().updateTeamPoints(allTeams, gameNo);
    }


    public void updateTeamRankings (boolean lastGameOnly, int lastPlayedGameNo) {
        server.getDb().updateTeamRankings(lastGameOnly, lastPlayedGameNo);
    }


    public String[] fillComboGames() {

        curr = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String datenow = ""+formatter;
        String toParse = server.getDb().getGames(10, datenow);
        String temp = toParse.substring(1, toParse.length() - 1);
        String parsed[] = temp.split("><");
        int n = Integer.parseInt(parsed[1]);
        if (n==0){
            System.out.println("No games available.");
            return null;
        }
        else{
            int start = 3;
            String[] result = new String[n];
            for (int i = 0; i<n; i++) {
                result[i] = parsed[start]+" - "+parsed[start+1];
                start+=11;
            }
            return result;
        }
        
    }


    public ArrayList getGameScores(int gameNo){

        String toParse = server.getDb().getGames(1, gameNo);
        String temp = toParse.substring(1, toParse.length() - 1);
        String parsed[] = temp.split("><");
        ArrayList<Integer> A = new ArrayList<Integer>();
        A.add(Integer.parseInt(parsed[9]));
        A.add(Integer.parseInt(parsed[10]));
        return A;
        
    }

    public String getGameEvents(int gameNo){

        String toParse = server.getDb().getGames(1, gameNo);
        String temp = toParse.substring(1, toParse.length() - 1);
        String parsed[] = temp.split("><");
        return parsed[11];
        
    }

    public String[] getGameTeams(int gameNo){

        String toParse = server.getDb().getGames(1, gameNo);
        String temp = toParse.substring(1, toParse.length() - 1);
        String parsed[] = temp.split("><");
        String[] names = new String[2];
        names[0] = parsed[3];
        names[1] = parsed[4];
        return names;
    }

//    public void updateGameDate(int index) {
//        curr = Calendar.getInstance();
//        server.setGame(index,curr);
//    }

}




